/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.code.config;

import jakarta.persistence.Index;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.Table;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 挺好的 2023年06月06日 16:35
 */
public class AnnotationUtils {

    /**
     * 实体类
     */
    private Class entityClass;

    private volatile static AnnotationUtils instance;

    private AnnotationUtils (Class entityClass) {
        this.entityClass = entityClass;
    }

    public static AnnotationUtils getInstance (Class entityClass) {
        if (instance == null) {
            synchronized (AnnotationUtils.class) {
                if (instance == null) {
                    instance = new AnnotationUtils(entityClass);
                }
            }
        }
        return instance;
    }

    /**
     * 获取 NamedEntityGraph
     *
     * @return NamedEntityGraph 的名称。如果没有找到，返回EMPTY
     */
    public String getNamedEntityGraph () {

        NamedEntityGraph namedEntityGraph = (NamedEntityGraph) this.entityClass.getAnnotation(NamedEntityGraph.class);

        if (namedEntityGraph == null) {
            return StringUtils.EMPTY;
        }

        return namedEntityGraph.name();
    }


    /**
     * 获取表的注释
     *
     * @return 表注释。如果没有找到，返回EMPTY
     */
    public String getTableComment () {
        return "";
        //        TableComment tableComment = (TableComment) this.entityClass.getDeclaredAnnotation(TableComment.class);
        //
        //        if (tableComment == null) {
        //            return StringUtils.EMPTY;
        //        }
        //
        //        return tableComment.value();
    }

    /**
     * 获取表的名称
     *
     * @return 表名称。如果没有找到，返回EMPTY
     */
    public String getTableName () {
        Table table = (Table) this.entityClass.getDeclaredAnnotation(Table.class);

        if (table == null) {
            return StringUtils.EMPTY;
        }

        return table.name();
    }

    /**
     * 获取表的索引
     *
     * @return
     */
    public List <IndexField> getIndex () {
        Table table = (Table) this.entityClass.getDeclaredAnnotation(Table.class);
        if (table == null) {
            return Collections.emptyList();
        }

        List <IndexField> indexList = new ArrayList <>();

        Index[] indexes = table.indexes();

        if (ArrayUtils.isNotEmpty(indexes)) {

            for (Index index : indexes) {
                IndexField indexField = new IndexField();
                indexField.setName(index.name());
                indexField.setIsUnique(index.unique());

                String columnList = index.columnList();

                List <String> columns = new ArrayList <>();

                if (StringUtils.contains(columnList, ",")) {

                    String[] columnArray = StringUtils.split(columnList, ",");

                    for (String col : columnArray) {
                        String humpToUnderline = humpToUnderline(col);
                        columns.add(humpToUnderline);
                    }
                } else {
                    String humpToUnderline = humpToUnderline(columnList);
                    columns.add(humpToUnderline);
                }

                indexField.setColumns(columns);

                indexList.add(indexField);
            }
        }

        return indexList;
    }

    public static String humpToUnderline (String humpName) {
        //截取下划线分成数组，

        char[] charArray = humpName.toCharArray();

        StringBuffer buffer = new StringBuffer();

        //处理字符串

        for (int i = 0, l = charArray.length; i < l; i++) {
            if (charArray[i] >= 65 && charArray[i] <= 90) {
                buffer.append("_").append(charArray[i] += 32);

            } else {
                buffer.append(charArray[i]);

            }

        }

        return buffer.toString();
    }
}
